import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/regional_organization_tree/regional_organization_tree_view.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/customEndDrawer.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/common/risk_tree_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/double_control/get_count_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/double_control/list_ledger_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/double_prevention_page/danger_investigation_page/danger_investigation_controller.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';

/// 隐患治理台账
class HiddenDangerControlController extends GetxController {
  /// 列表刷新key
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();

  /// 选中的节点
  RiskTreeEntity selectedEntity = RiskTreeEntity();

  /// 筛选后的标题
  String? title = '全部';

  ///隐患数量统计 数据
  GetCountEntity? getCountEntity;

  /// 筛选右侧栏控制Key
  final riskRightScaffoldKey = GlobalKey<ScaffoldState>();

  ///风险分析对象ID
  String riskObjectId = '';

  ///风险分析单元ID
  String riskUnitId = '';

  ///风险事件ID
  String riskEventId = '';

  @override
  void onInit() {
    super.onInit();
    _getCount();
  }

  /// 筛选按钮
  void onFilterButtonPressed() {
    final DangerInvestigationController controller = Get.find<DangerInvestigationController>();
    controller.filterWidget.value = _filterWidget();
    controller.riskRightScaffoldKey.currentState?.openEndDrawer();
    // riskRightScaffoldKey.currentState?.openEndDrawer();
  }

  // 跳转隐患详情
  void onHiddenDangerDetailsButtonPressed(String id) {
    Get.toNamed(AppRoutes.hiddenDiseaseDetails, arguments: id);
  }

  Future<List<ListLedgerRecords>> getRiskSecurityIdentificationList(int index, int size, {String? search}) async {
    final res = await HttpUtil().generateGetRequest<List<ListLedgerRecords>>(
      Interface.listLedger,
      queryParameters: {
        'pageNum': index,
        'pageSize': size,
        'riskObjectId': riskObjectId,
        'riskUnitId': riskUnitId,
        'riskEventId': riskEventId,
      },
    ).execute();
    List<ListLedgerRecords>? list = [];
    res.onSuccess((result) {
      if (result.data != null) {
        list = result.data;
      }
    });
    update();
    return list!;
  }

  ///隐患数量统计
  void _getCount() async {
    final res = await HttpUtil().generateGetRequest<GetCountEntity>(Interface.getCount).execute();
    res.onSuccess((result) {
      if (result.data != null) {
        getCountEntity = result.data!;
        update();
      }
    });
  }

  // 重置
  void reset() {
    final DangerInvestigationController controller = Get.find<DangerInvestigationController>();
    riskObjectId = '';
    riskUnitId = '';
    riskEventId = '';
    controller.riskRightScaffoldKey.currentState?.closeEndDrawer();
    selectedEntity = RiskTreeEntity();
    globalKey.currentState!.onRefresh();
    title = '全部';
    update();
  }

  // 确定
  void confirm() {
    Get.back();
    //修改当前安全风险等级level
    if (selectedEntity.level == 1) {
      title = '全部';
      riskObjectId = '';
      riskUnitId = '';
      riskEventId = '';
    } else {
      if (selectedEntity.level == 2) {
        //对象
        title = '全部/${selectedEntity.name}';
        riskObjectId = selectedEntity.id!;
        riskUnitId = '';
        riskEventId = '';
        title = '全部/${selectedEntity.name}';
        globalKey.currentState!.onRefresh();
        update();
        return;
      } else if (selectedEntity.level == 3) {
        //单元
        riskObjectId = '';
        riskUnitId = selectedEntity.id!;
        riskEventId = '';
      } else if (selectedEntity.level == 4) {
        //事件
        riskObjectId = '';
        riskUnitId = '';
        riskEventId = selectedEntity.id!;
      }
      title = '${selectedEntity.path}/${selectedEntity.name}';
      if (title != null && title!.startsWith('/')) {
        title = title?.substring(1, title?.length);
        title = '全部/$title';
      }
    }
    globalKey.currentState!.onRefresh();
    update();
  }

  Widget _filterWidget() {
    return CustomEndDrawer(
      centerContext: Column(
        children: [
          RegionalOrganizationTreePage(
            selectedEntity: selectedEntity,
            onCallBack: (RiskTreeEntity entity) {
              selectedEntity = entity;
            },
          ),
        ],
      ),
      reset: reset,
      confirm: confirm,
    );
  }
}
